package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.AttentionIntegralListRequest;
import com.frogs.ucenter.v1.request.IntegralConsumeRatioRequest;
import com.frogs.ucenter.v1.request.IntegralConsumeRecordListRequest;
import com.frogs.ucenter.v1.request.IntegralConsumeRecordRequest;
import com.frogs.ucenter.v1.request.IntegralExchangeCDkeyRequest;
import com.frogs.ucenter.v1.request.IntegralExchangeCouponRequest;
import com.frogs.ucenter.v1.request.IntegralExchangeRequest;
import com.frogs.ucenter.v1.request.UserIntegralBalanceAddByConsumeRequest;
import com.frogs.ucenter.v1.request.UserIntegralBalanceAddRequest;
import com.frogs.ucenter.v1.request.UserIntegralBalanceBackByConsumeRequest;
import com.frogs.ucenter.v1.request.UserIntegralBalanceBlockingRequest;
import com.frogs.ucenter.v1.request.UserIntegralBalanceReleaseRequest;
import com.frogs.ucenter.v1.request.UserIntegralBalanceShowRequest;
import com.frogs.ucenter.v1.request.UserIntegralBalanceUnfreezeRequest;
import com.frogs.ucenter.v1.request.UserIntegralConsumeBackRequest;
import com.frogs.ucenter.v1.request.UserIntegralConsumeRecordListRequest;
import com.frogs.ucenter.v1.request.UserIntegralRecordListRequest;
import com.frogs.ucenter.v1.service.UserIntegralBalanceService;
import com.frogs.ucenter.v1.service.UserIntegralConsumeRecordService;
import com.frogs.ucenter.v1.service.UserIntegralExchangeService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "userIntegralController")
@RequestMapping("/" + ApiConstants.VERSION )
public class UserIntegralController extends BaseController3 {
	
	@Resource(name = ApiConstants.VERSION + "userIntegralBalanceService")
	private UserIntegralBalanceService userIntegralBalanceService;
	
	@Resource(name = ApiConstants.VERSION + "userIntegralExchangeService")
	private UserIntegralExchangeService userIntegralExchangeService;
	
	@Resource(name = ApiConstants.VERSION + "userIntegralConsumeRecordService")
	private UserIntegralConsumeRecordService userIntegralConsumeRecordService;

	//用户（个人）积分
	private static final String ROOT = "/user/integral";
	//订单积分
	private static final String ROOT_ORDER = "/order/integral";
	//家庭成员积分列表
	private static final String USER_ATTENTION_INTEGRAL_LIST = "/user/integral/attention/list";
	//积分商城
	private static final String ROOT_INTEGRAL = "/integral";
	private static final String USER_INTEGRAL_BALANCE_DETAIL = ROOT + DETAIL;
	private static final String USER_INTEGRAL_BALANCE_ADD_BY_CONSUME = ROOT + "/addbyconsume";
	private static final String USER_INTEGRAL_BALANCE_BACK_BY_CONSUME = ROOT + "/backbyconsume";
	private static final String USER_INTEGRAL_BALANCE_ADD = ROOT + ADD;
	private static final String USER_INTEGRAL_CONSUME_RATIO = ROOT + "/consume/ratio";
	private static final String USER_INTEGRAL_CONSUME_BLOCKING = ROOT + "/consume/blocking";       //兑换请求，冻结积分
	private static final String USER_INTEGRAL_CONSUME_UNFREEZE = ROOT_ORDER + "/consume/unfreeze";  //兑换失败，返还积分
	private static final String USER_INTEGRAL_CONSUME_RELEASE = ROOT_ORDER + "/consume/release";   //兑换成功， 消费积分
	private static final String USER_INTEGRAL_CONSUME_BACK = ROOT_ORDER + "/consume/back";
	private static final String USER_INTEGRAL_COUPON_EXCHANGE = ROOT + "/coupon/exchange";
	private static final String USER_INTEGRAL_GOODS_EXCHANGE = ROOT + "/goods/exchange";
	private static final String USER_INTEGRAL_CDKEY_EXCHANGE = ROOT + "/cdkey/exchange";
	private static final String USER_INTEGRAL_CONSUME_LIST =  ROOT + "/consume/list";	
	private static final String USER_INTEGRAL_LIST =  ROOT + "/list";	
	private static final String INTEGRAL_EXCHANGE_LIST = ROOT_INTEGRAL + "/exchange/list";
	private static final String INTEGRAL_CONSUME_LIST = ROOT_INTEGRAL + "/consume/list";
	private static final String INTEGRAL_CONSUME_ORDERSN = ROOT + "/consume/orderSn";   //获取兑换订单详情
	
	@RequestMapping(USER_INTEGRAL_BALANCE_DETAIL)
	public Response userIntegralBalanceDetail(HttpServletRequest httpRequest) {
		Request request = new UserIntegralBalanceShowRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userIntegralBalanceService.showUserIntegralBalance(request);
			response.setTip("获取账户积分成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}

		return response;
	}
	//根据消费金额登记积分
	@RequestMapping(USER_INTEGRAL_BALANCE_ADD_BY_CONSUME)
	public Response userIntegralBalanceAddByConsume(HttpServletRequest httpRequest) {
		Request request = new UserIntegralBalanceAddByConsumeRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userIntegralBalanceService.addByConsumeUserIntegralBalance(request);
			response.setTip("添加积分成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	//根据消费金额回滚积分
	@RequestMapping(USER_INTEGRAL_BALANCE_BACK_BY_CONSUME)
	public Response userIntegralBalanceBackByConsume(HttpServletRequest httpRequest) {
		Request request = new UserIntegralBalanceBackByConsumeRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userIntegralBalanceService.backByConsumeUserIntegralBalance(request);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	//根据积分值登记积分
	@RequestMapping(USER_INTEGRAL_BALANCE_ADD)
	public Response userIntegralBalanceAdd(HttpServletRequest httpRequest) {
		Request request = new UserIntegralBalanceAddRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userIntegralBalanceService.addUserIntegralBalance(request);
			response.setTip("添加积分成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 获取兑换优惠券和商品
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(INTEGRAL_EXCHANGE_LIST)
	public Response integralExchangeList(HttpServletRequest request) {
		Response response;
		try {
			IntegralExchangeRequest exchangeRequest = new IntegralExchangeRequest();
			exchangeRequest.parse(request);
			response = userIntegralExchangeService.getIntegralExchangeList(exchangeRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 获取用户积分和兑换比例
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_INTEGRAL_CONSUME_RATIO)
	public Response userIntegralConsumeRatio(HttpServletRequest request) {
		Response response;
		try {
			IntegralConsumeRatioRequest consumeRatioRequest = new IntegralConsumeRatioRequest();
			consumeRatioRequest.parse(request);
			response = userIntegralExchangeService.getIntegralConsumeRatio(consumeRatioRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 冻结积分
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_INTEGRAL_CONSUME_BLOCKING)
	public Response userIntegralConsumeBlocking(HttpServletRequest request) {
		Response response;
		try {
			UserIntegralBalanceBlockingRequest blockingRequest = new UserIntegralBalanceBlockingRequest();
			blockingRequest.parse(request);
			response = userIntegralExchangeService.blockingIntegralBalance(blockingRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 解冻积分
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_INTEGRAL_CONSUME_UNFREEZE)
	public Response userIntegralConsumeUnfreeze(HttpServletRequest request) {
		Response response;
		try {
			UserIntegralBalanceUnfreezeRequest unfreezeRequest = new UserIntegralBalanceUnfreezeRequest();
			unfreezeRequest.parse(request);
			response = userIntegralExchangeService.unfreezeIntegralBalance(unfreezeRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 消除冻结积分
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_INTEGRAL_CONSUME_RELEASE)
	public Response userIntegralConsumeRelease(HttpServletRequest request) {
		Response response;
		try {
			UserIntegralBalanceReleaseRequest releaseRequest = new UserIntegralBalanceReleaseRequest();
			releaseRequest.parse(request);
			response = userIntegralExchangeService.releaseBlockingBalance(releaseRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 积分抵制消费余额回滚
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_INTEGRAL_CONSUME_BACK)
	public Response userIntegralConsumeBack(HttpServletRequest request) {
		Response response;
		try {
			UserIntegralConsumeBackRequest backRequest = new UserIntegralConsumeBackRequest();
			backRequest.parse(request);
			response = userIntegralExchangeService.integralConsumeBack(backRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 获取积分消费记录
	 * 注：此处为了绕开登录验证
	 * @param request
	 * @return
	 */
	@RequestMapping(INTEGRAL_CONSUME_LIST)
	public Response integralConsumeList(HttpServletRequest request) {
		Response response;
		try {
			IntegralConsumeRecordListRequest consumeRecordListRequest = new IntegralConsumeRecordListRequest();
			consumeRecordListRequest.parse(request);
			response = userIntegralConsumeRecordService.getIntegralConsumeRecordList(consumeRecordListRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 *  获取用户积分消费记录
	 *  
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_INTEGRAL_CONSUME_LIST)
	public Response userIntegralConsumeList(HttpServletRequest request) {
		Response response;
		try {
			UserIntegralConsumeRecordListRequest consumeRecordListRequest = new UserIntegralConsumeRecordListRequest();
			consumeRecordListRequest.parse(request);
			response = userIntegralConsumeRecordService.getUserIntegralConsumeRecordList(consumeRecordListRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 *  积分兑换优惠券
	 *  
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_INTEGRAL_COUPON_EXCHANGE)
	public Response userIntegralCouponExchange(HttpServletRequest request) {
		Response response;
		try {
			IntegralExchangeCouponRequest exchangeCouponRequest = new IntegralExchangeCouponRequest();
			exchangeCouponRequest.parse(request);
			response = userIntegralExchangeService.integralExchangeCoupon(exchangeCouponRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 积分兑换商品（未实现）
	 */
	@RequestMapping(USER_INTEGRAL_GOODS_EXCHANGE)
	public Response exchangeGoods(HttpServletRequest request){
		return null;
	}
	/**
	 *  获取用户的积分获取列表
	 *  
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_INTEGRAL_LIST)
	public Response userIntegralList(HttpServletRequest request) {
		Response response;
		try {
			UserIntegralRecordListRequest userIntegralRecordListRequest = new UserIntegralRecordListRequest();
			userIntegralRecordListRequest.parse(request);
			response = userIntegralBalanceService.getUserIntegralRecordList(userIntegralRecordListRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 *  积分兑换激活码
	 *  
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_INTEGRAL_CDKEY_EXCHANGE)
	public Response userIntegralCDkeyExchange(HttpServletRequest request) {
		Response response;
		try {
			IntegralExchangeCDkeyRequest exchangeCDkeyRequest = new IntegralExchangeCDkeyRequest();
			exchangeCDkeyRequest.parse(request);
			response = userIntegralExchangeService.integralExchangeCDkey(exchangeCDkeyRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 *  获取家庭成员积分列表
	 *  
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_ATTENTION_INTEGRAL_LIST)
	public Response attentionIntegralList(HttpServletRequest request) {
		Response response;
		try {
			AttentionIntegralListRequest attentionIntegralListRequest = new AttentionIntegralListRequest();
			attentionIntegralListRequest.parse(request);
			response = userIntegralBalanceService.getAttentionIntegralList(attentionIntegralListRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(INTEGRAL_CONSUME_ORDERSN)
	public Response getIntegralConsumeByOrderSn(HttpServletRequest request){
		Response response;
		try {
			IntegralConsumeRecordRequest integralConsumeRecordRequest = new IntegralConsumeRecordRequest();
			integralConsumeRecordRequest.parse(request);
			response = userIntegralExchangeService.getIntegralConsumeByOrderSn(integralConsumeRecordRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
}